package com.example.demo2.controller;
import com.example.demo2.mapper.*;
import com.example.demo2.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@RestController
@CrossOrigin
public class UserController {

    @Autowired
    private UserMapper userMapper;

    private ConcurrentHashMap<String, Integer> concurrentMap;

    @Autowired
    public void MyController(ConcurrentHashMap<String, Integer> concurrentMap) {
        this.concurrentMap = concurrentMap;
    }

    //个人信息
    @GetMapping("/findUserById")
    public User findUserById() {
        Integer uID = (Integer) concurrentMap.get("uID");
        uID = 101;
        if(uID!=null)
            return userMapper.findUserById(uID);
        else
        {
            System.out.println("uID is null");
            return new User(-1,"哈哈哈哈",null,null,null,null,null);
        }
    }


    @GetMapping("/getURealname/{uId}")
    public String findRealname(@PathVariable Integer uId) {
        return userMapper.findUserById(uId).getURealName();
    }

    //个人信息修改
    @PostMapping("/updateAccount")
    public String updateAccount(@RequestBody Map<String,String> user) {
        Integer uID = (Integer) concurrentMap.get("uID");
        uID = 101;

        String realName = user.get("uRealName");
        boolean x=true;
        if(realName!=null&&realName.length()>0)
        {
            List<User> users = userMapper.findByuNm(realName);
            if(users!=null&&users.size()>0)
            {
                return "用户名不能重复";
            }
            x=x&userMapper.updateURealName(uID,realName);
        }
        String email = user.get("uEmail");
        if(email!=null&&email.length()>0)
        {
            x=x&userMapper.updateUEmail(uID,email);
        }
        String phone = user.get("uNum");
        if(phone!=null&&phone.length()>0)
        {
            x=x&userMapper.updateUNUM(uID,phone);
        }
        String company = user.get("uCompany");
        if(company!=null&&company.length()>0)
        {
            x=x&userMapper.updateUCompany(uID,company);
        }
        String pic = user.get("uPic");
        System.out.println(pic);
        if(pic!=null&&pic.length()>0)
        {

            x=x&userMapper.updateUPic(uID,pic);
        }
        if(x)
        {
            return "更新成功";
        }
        else
        {
            return "更新失败";
        }

    }

    //修改密码
    @PostMapping("/updateUserPw")
    public String updateUserPw(@RequestBody Map<String,String> pw) {
        String oldUPw = (String) pw.get("oldUPw");
        String newUPw = (String) pw.get("newUPw");
        System.out.println("原密码："+oldUPw);
        System.out.println("新密码："+newUPw);
        Integer uID = (Integer) concurrentMap.get("uID");
        uID = 101;

        if(userMapper.findPwdById(uID).equals(oldUPw))
        {
            if(userMapper.updateUserPw(uID,newUPw)==1)
            {
                return "修改成功";
            }
            else
            {
                return "修改失败";
            }
        }
        else return("原密码错误")
        ;
    }


}
